package com.farm.kaiping.controller;


import com.farm.kaiping.entity.Building;
import com.farm.kaiping.po.BuildingPO;
import com.farm.kaiping.service.BuildingLogicService;
import com.farm.kaiping.service.BuildingService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 厂房管理表(Building)表控制层
 *
 * @author makejava
 * @since 2025-01-22 13:57:24
 */
@RestController
@RequestMapping("/building")
@AllArgsConstructor
@Api(tags = "厂房管理表控制器")
public class BuildingController {

	private final BuildingService buildingService;
	private final BuildingLogicService buildingLogicService;

	@ApiOperation(value = "分页查询厂房管理表", notes = "权限：view_building_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_page')")
	@PostMapping("/page")
	public R<Page<Building>> getBuildingPage(@RequestBody BuildingPO buildingPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(buildingPO);
		return R.ok(buildingService.getBuildingPage(page, buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询厂房管理表列表", notes = "权限：view_building_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_list')")
	@PostMapping("/list")
	public R<List<Building>> getBuildingList(@RequestBody BuildingPO buildingPO) {
		try{
			return R.ok(buildingService.getBuildingList(buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询厂房管理表", notes = "权限 view_building_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_view')" )
	public R<Building> getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(buildingService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条厂房管理表", notes = "权限：view_building_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_one')")
	@PostMapping("/one")
	public R<BuildingPO> getBuilding(@RequestBody BuildingPO buildingPO) {
		try{
			return R.ok(buildingLogicService.getBuilding(buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增厂房管理表", notes = "权限：view_building_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_add')")
	@PostMapping("/add")
	public R<Integer> saveBuilding(@RequestBody BuildingPO buildingPO) {
		try{
		    return R.ok(buildingService.saveBuilding(buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改厂房管理表", notes = "权限：view_building_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_edit')")
	@PostMapping("/edit")
	public R<String> updateBuilding(@RequestBody BuildingPO buildingPO) {
		try{
		    return R.ok(buildingService.updateBuilding(buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除厂房管理表", notes = "权限：view_building_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_building_del')")
	@PostMapping("/del")
	public R<String> removeBuilding(@RequestBody BuildingPO buildingPO) {
		try{
			buildingLogicService.checkBeforeRemove(buildingPO);
		    return R.ok(buildingService.removeBuilding(buildingPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}
	
}

